//
//  ViewController.m
//  11207-足球队
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong,nonatomic) NSDictionary *dictName;

@property (strong,nonatomic) NSArray *keyArr;

@property (strong,nonatomic) UISearchController *searchController;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"足球队dictionary.plist" ofType:nil];
    self.dictName = [NSDictionary dictionaryWithContentsOfFile:path];
    self.keyArr = [[self.dictName allKeys] sortedArrayUsingSelector:@selector(compare:)];
    //添加搜索控制器
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.tableView.tableHeaderView = self.searchController.searchBar;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.keyArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = self.keyArr[section];
    NSArray *arr = self.dictName[key];
    return arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *key = self.keyArr[indexPath.section];
    NSArray *arr = self.dictName[key];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
    }
    cell.textLabel.text = arr[indexPath.row];
    
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.keyArr[section];
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.keyArr;
}

#pragma mark - 处理搜索数据
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
